package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dto.AccountDto;
import com.example.dto.ProNormDto;
import com.example.dto.VipDto;
import com.example.exception.MyException;
import com.example.service.VipService;
import com.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/vip")
public class VipController {
    @Autowired
    private VipService service;

    @RequestMapping("add")
    public Result add(@RequestBody VipDto dto) {
        dto.setAddTime(LocalDateTime.now());
        System.out.println(dto);
        service.add(dto);
        return Result.success("注册成功").put("phone", dto.getPhone()).put("password", dto.getPassword());
    }

    @RequestMapping("select")
    public IPage<VipDto> select(@RequestBody VipDto dto) {
        IPage<VipDto> page = service.select(dto);
        return page;
    }


    @RequestMapping("/login")
    public Result login(@RequestBody VipDto dto) {
        try {
            String vipToken = service.login(dto);
            return Result.success().put("vipToken", vipToken);
        } catch (MyException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    @RequestMapping("update")
    public Object update(@RequestBody VipDto dto) {
        service.update(dto);
        return null;
    }

    @RequestMapping("delete")
    public Object delete(@RequestBody VipDto dto) {
        service.delete(dto);
        return null;
    }

    @RequestMapping("sendMsg/{phoneCode}")
    public Result sendMsg(@PathVariable("phoneCode") String phoneCode) {
        System.out.println("进入发短信方法");
        try {
//            发短信
            service.sendMsg(phoneCode);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

}
